/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package carreras.datos.connection;

import carreras.Configuracion;
import java.io.FileNotFoundException;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 *
 * @author fanky10<fanky@gmail.com>
 */
public class SQLite extends DBConnector{
    private static final String SQLITE_STR_CNN = "jdbc:sqlite:";
    private static final String SQLITE_DRIVER_CLASS = "org.sqlite.JDBC";
    
    private static final String DEFAULT_DB = System.getProperty("user.dir")+java.io.File.separator+"db.sqlite";
    private String db_file;
    
    public SQLite() throws  ClassNotFoundException, InstantiationException, IllegalAccessException{
        this(Configuracion.getDBProperties());

    }
    public SQLite(Properties props) throws  ClassNotFoundException, InstantiationException, IllegalAccessException{
        db_file = (props.getProperty("database",DEFAULT_DB));
        Class.forName(SQLITE_DRIVER_CLASS).newInstance();
        statementV = new Statements();
    }
    @Override
    public String toString(){
        return getURLConn();
    }
    public synchronized void conectar() throws SQLException{
        if(conn==null || conn.isClosed()){
            conn = DriverManager.getConnection(getURLConn());
            System.out.println("SQlite instance connected on: "+toString());
//            notifyAll();
        }
    }

    @Override
    public synchronized ResultSet getResultSet(String query) throws SQLException {
        debug("creating rs: "+query);
        return getStmt().executeQuery(query);
    }

    @Override
    protected synchronized Statement getStmt() throws SQLException {
        statement = conn.createStatement();
        debug(" statement CREATED notificando...");
        statementV.add(statement);
        return statement;
    }

    private String getURLConn(){
        return SQLITE_STR_CNN+ db_file;
    }

    @Override
    public synchronized int executeUpdate(String non_query) throws SQLException {
        debug("executing upd: "+non_query);
        return getStmt().executeUpdate(non_query);
    }

    @Override
    public synchronized void closeConnection() throws SQLException {
        debug("conn closed notify");
        conn.close();
    }

    @Override
    public synchronized boolean connectionIsClosed() throws SQLException {
        if(conn!=null){
            return conn.isClosed();
        }
        //si es nulo entonces ni la abrio
        return true;
    }

    @Override
    public synchronized void startTransaction() throws SQLException {

        setAutoCommit(false);
        debug("starting trans");
    }

    @Override
    public synchronized void commitTransaction() throws SQLException {
        conn.commit();
        debug("commit trans"); //si no muestra esto es xq nunca hizo commit
        setAutoCommit(true);
    }

    @Override
    public synchronized void rollbackTransaction() throws SQLException {
        conn.rollback();
        debug("rollback trans");
        setAutoCommit(true);
    }

    @Override
    public synchronized void setAutoCommit(boolean aBool) throws SQLException {
        conn.setAutoCommit(aBool);
    }

    @Override
    public synchronized void closeStmt() throws SQLException {
        //no se cierran los statements
        for(Statement st: statementV){
            try{
//                if(st!=null && !st.isClosed()){
                    st.close();
                    debug("CERRADO el statement... y notificando");
//                }
            }catch(SQLException ex){
                debug("exception tratando de cerrar un statement... "+ex.getMessage());
            }
        }
        statementV.clear();
    }

    @Override
    public synchronized void closeResultSet() throws SQLException {
        //si los result set.. pero vamos a ver que onda :P
//        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean isAutoCommit() throws SQLException {
        return conn.getAutoCommit();
    }
}
